package com.finer.service.impl;

import com.finer.dao.FinerGoodsDao;
import com.finer.entity.ResponseMessage;
import com.finer.entity.document.FinerGoodUnit;
import com.finer.dao.FinerGoodUnitDao;
import com.finer.service.FinerGoodUnitService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 商品单位表(FinerGoodUnit)表服务实现类
 *
 * @author makejava
 * @since 2024-10-21 10:20:37
 */
@Service("finerGoodUnitService")
@Slf4j
@Transactional
public class FinerGoodUnitServiceImpl implements FinerGoodUnitService {
    @Resource
    private FinerGoodUnitDao finerGoodUnitDao;

    @Resource
    private FinerGoodsDao finerGoodsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ResponseMessage queryById(Integer id) {
        log.info("FinerGoodUnitServiceImpl queryById param:{}",id);
        ResponseMessage msg = new ResponseMessage();
        FinerGoodUnit finerGoodUnit = this.finerGoodUnitDao.queryById(id);
        if (finerGoodUnit == null) {
            msg.setCode("500");
            msg.setMessage("计量单位不存在");
        }else{
            msg.setCode("200");
            msg.setData(finerGoodUnit);
        }
        return msg;
    }



    /**
     * 修改数据
     *
     * @param finerGoodUnit 实例对象
     * @return 实例对象
     */
    @Override
    public ResponseMessage update(FinerGoodUnit finerGoodUnit) {
        log.info("FinerGoodUnitServiceImpl update param:{}", finerGoodUnit);
        ResponseMessage msg = new ResponseMessage();
        //判断单位名称是否重复
        int count = finerGoodUnitDao.getUnitCountByUnitName(finerGoodUnit);
        if (count > 0) {
            msg.setCode("501");
            msg.setMessage("单位名称重复，请修改！");
            return msg;
        }
        boolean flag = this.finerGoodUnitDao.update(finerGoodUnit)>0;
        if (flag) {
            msg.setCode("200");
            msg.setMessage("修改成功");
        }else{
            msg.setCode("500");
            msg.setMessage("修改失败");
        }
        return msg;
    }


    @Override
    public ResponseMessage selByPage(Map map) {
        log.info("FinerGoodUnitServiceImpl selByPage param:{}",map);
        ResponseMessage msg = new ResponseMessage();
        PageHelper.startPage(Integer.parseInt(map.get("pageNum").toString()),Integer.parseInt(map.get("pageSize").toString()));
        List<FinerGoodUnit> finerGoodUnits = finerGoodUnitDao.selAllUnit(map);
        log.info("NewsDetailServiceImpl newsDetailList result:{}",finerGoodUnits);
        PageInfo<FinerGoodUnit> pageInfo = new PageInfo<>(finerGoodUnits);
        msg.setData(pageInfo);
        msg.setCode("200");
        return msg;
    }

    @Override
    public ResponseMessage addUnit(FinerGoodUnit finerGoodUnit) {
        log.info("FinerGoodUnitServiceImpl addUnit param:{}",finerGoodUnit);
        ResponseMessage msg = new ResponseMessage();
        //判断单位名称是否重复
        int count = finerGoodUnitDao.getUnitCountByUnitName(finerGoodUnit);
        if (count > 0) {
            msg.setCode("501");
            msg.setMessage("单位名称重复，请修改！");
            return msg;
        }
        boolean flag = finerGoodUnitDao.insert(finerGoodUnit)>0;
        if (flag){
            msg.setCode("200");
            msg.setMessage("添加单位成功！");
        }else{
            msg.setCode("500");
            msg.setMessage("添加失败");
        }
        return msg;
    }

    @Override
    public ResponseMessage delUnitById(Integer id) {
        log.info("FinerGoodUnitServiceImpl delUnitById param:{}",id);
        ResponseMessage msg = new ResponseMessage();
        //需要判断商品表中是否使用了该id对应的计量单位，有则不能删除
        int goodsCount = finerGoodsDao.getGoodsCountByUnitId(id);
        if (goodsCount>0){
            msg.setCode("501");
            msg.setMessage("该单位还有对应的商品在使用，不能删除！");
            return msg;
        }
        boolean flag = finerGoodUnitDao.deleteById(id)>0;
        if (flag){
            msg.setCode("200");
            msg.setMessage("删除成功");
        }else{
            msg.setCode("500");
            msg.setMessage("删除失败");
        }
        return msg;
    }
}
